﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNNspot.LiveChat.DataModel
{
    public class MessageInfo
    {
        public int? AgentId { get; set; }
        public string Body { get; set; }
        public int? ClientId { get; set; }
        public DateTime? CreatedDate { get; set; }
        public int? MessageId { get; set; }
        public string Name { get; set; }
        public int? PortalId { get; set; }
        public int? SessionId { get; set; }

        public MessageInfo(Message message)
        {
            AgentId = message.AgentId;
            Body = message.Body;
            ClientId = message.ClientId;
            CreatedDate = message.CreatedDate;
            MessageId = message.MessageId;
            PortalId = message.PortalId;
            SessionId = message.SessionId;

            if(message.AgentId != null)
            {
                var agent = new Agent();
                if(agent.LoadByPrimaryKey(Convert.ToInt32(message.AgentId)))
                {
                    Name = agent.UpToUserByUserId.DisplayName;
                }
            }
            else if(message.ClientId != null)
            {
                var client = new Client();
                if (client.LoadByPrimaryKey(Convert.ToInt32(message.ClientId)))
                {
                    Name = string.Format("{0} {1}", client.FirstName, client.LastName.Substring(0, 1));
                }
            }
        }
    }
}
